package com.vitabuybuy.tv.ui.mine.account

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import androidx.core.os.bundleOf
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.mmkv
import com.library.common.ext.setOnclickNoRepeat

import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivitySmsVerifyBinding
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.mine.account.viewmodel.SmsVerifyViewModel
import com.library.common.util.MMKVContancts
import com.vitabuybuy.tv.dialog.AreaCodePopup
import kotlinx.android.synthetic.main.activity_find_password.*
import kotlinx.android.synthetic.main.activity_sms_verify.*
import kotlinx.android.synthetic.main.activity_sms_verify.countryCodePicker
import kotlinx.android.synthetic.main.activity_sms_verify.etPhone
import kotlinx.android.synthetic.main.activity_sms_verify.etSmsCode
import kotlinx.android.synthetic.main.activity_sms_verify.tvGetSmsCode

class SmsVerifyActivity : BaseActivity<SmsVerifyViewModel, ActivitySmsVerifyBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_sms_verify

    private var isGetCode = false
    private var defaultAreaCode = "86"

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.sms_verify))
        if (mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
            etPhone.setText(mmkv.decodeString(MMKVContancts.USER_PHONE, ""))
            etPhone.isEnabled = false
        }
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvGetSmsCode, btnConfirm){v->
            if (etPhone.text.isEmpty()) {
                XToast.normal(getString(R.string.enter_phone_number))
                return@setOnclickNoRepeat
            }
            if (mmkv.decodeBool(MMKVContancts.IS_LOGIN_IN, false)) {
                if (etPhone.text.toString() != mmkv.decodeString(MMKVContancts.USER_PHONE, "")) {
                    XToast.normal(getString(R.string.pls_user_current_phone))
                    return@setOnclickNoRepeat
                }
            }
            when (v) {
                tvGetSmsCode -> {
                    viewModel.sendToPhone(
                        etPhone.text.toString(), defaultAreaCode
                    )
                }
                btnConfirm -> {
                    when {
                        !isGetCode -> XToast.normal(getString(R.string.please_get_code))
                        etSmsCode.text.isEmpty() -> XToast.normal(getString(R.string.enter_code))
                        else -> viewModel.validateByPhone(
                            etPhone.text.toString(),
                            etSmsCode.text.toString(),
                            defaultAreaCode
                        )
                    }
                }
            }
        }
        countryCodePicker.setOnClickListener{
            AreaCodePopup(this){
                countryCodePicker.text = it
                defaultAreaCode = it.replace("+","")
            }.showPopupWindow()
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.already_sms_code))
                timer.start()
            }
            2 -> {
                ThirdPartyBindActivity.openActivity(
                    mContext,
                    bundleOf(Pair("from", ThirdPartyBindActivity.FROM_SMSVERIFY))
                )
            }
        }
    }

    private val timer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            isGetCode = true
            mBinding.tvGetSmsCode.text = String.format(
                getString(R.string.get_sms_code_s),
                millisUntilFinished / 1000
            )
            mBinding.tvGetSmsCode.isEnabled = false
        }

        override fun onFinish() {
            mBinding.tvGetSmsCode.text = getString(R.string.get_sms_code)
            mBinding.tvGetSmsCode.isEnabled = true
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, SmsVerifyActivity::class.java)
        }
    }
}
